import { createContext, useCallback, useEffect, useState } from 'react';

export type LoginContextType = {
    /**表单的实例 */
    loginForm: any;
    /**设置表单实例 */
    setloginForm: Function;
    /**是否同意隐私协议 */
    isAgreeAgreement: boolean;
    /**设置是否同意隐私协议 */
    setIsAgreeAgreement: Function;
    /**图片验证码 */
    imgCaptcha: string;
    /**设置图片验证码 */
    setImgCaptcha: Function;
    /**是否显示图片验证码弹框 */
    isShowImgCaptchaModal: boolean;
    /**打开图片验证码弹框 */
    openImgCaptcha: () => Promise<unknown>;
    //关闭图片验证码弹框
    closeImgCaptcha: (params: boolean) => void;
};
export const LoginContext = createContext<LoginContextType>(undefined);
const useLogin = () => {
    const [endOpenImgCaptcha, setEndOpenImgCaptcha] = useState(undefined);
    const [loginForm, setloginForm] = useState(undefined);
    const [isAgreeAgreement, setIsAgreeAgreement] = useState(false);
    const [isShowImgCaptchaModal, setIsShowImgCaptchaModal] = useState(false);
    const [imgCaptcha, setImgCaptcha] = useState('');
    const openImgCaptcha =useCallback( async () => {
        setIsShowImgCaptchaModal(true);
        let promise = new Promise((resolve) => {
            setEndOpenImgCaptcha(() => resolve);
        });
        return promise;
    },[])
    const closeImgCaptcha = useCallback((isConfirm: boolean) => {
        //关闭弹框
        setIsShowImgCaptchaModal(false);
        //设置是否点了确定
        endOpenImgCaptcha(isConfirm);
    },[endOpenImgCaptcha])

    return {
        loginForm,
        setloginForm,
        imgCaptcha,
        setImgCaptcha,
        isAgreeAgreement,
        setIsAgreeAgreement,
        isShowImgCaptchaModal,
        setIsShowImgCaptchaModal,
        openImgCaptcha,
        closeImgCaptcha,
    };
};

export { useLogin };
